package com.blogSystem.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.blogSystem.user.dao.AdminDao;
import com.blogSystem.user.pojo.Admin;
import com.blogSystem.user.service.AdminService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;
import util.JwtUtil;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
    final public static int SIZE=10;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private AdminService adminService;
    @Autowired
    private HttpSession session;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Result login(@RequestBody Admin admin){

        Admin adminLogin=adminService.login(admin);
        if(adminLogin==null){
            return new Result(false , StatusCode.LOGINERROR, "用户名或密码错误");
        }
        else {
            System.out.println(adminLogin.getRole());

            String token=jwtUtil.createJWT(adminLogin.getId(), adminLogin.getName(), adminLogin.getRole());
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("token",token);
            map.put("role",adminLogin.getRole());
            return new Result(true, StatusCode.OK, "登录成功",map);
        }

    }


}
